package com.bxd.baodan.util;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;

import com.bxd.baodan.App;
import com.bxd.baodan.app.LoginInfo;
import com.bxd.baodan.item.UserItem;
import com.tencent.TIMCallBack;
import com.tencent.qcloud.presentation.business.InitBusiness;
import com.tencent.qcloud.presentation.business.LoginBusiness;
import com.tencent.qcloud.presentation.event.FriendshipEvent;
import com.tencent.qcloud.presentation.event.GroupEvent;
import com.tencent.qcloud.presentation.event.MessageEvent;
import com.tencent.qcloud.timchat.utils.PushUtil;
import com.xiaomi.mipush.sdk.MiPushClient;

import java.util.List;

import static android.content.Context.NOTIFICATION_SERVICE;
import static com.tencent.open.utils.Global.getPackageName;

/**
 * Created by Administrator on 2017/5/17.
 */

public class LoginIMUtil {
    private Activity activity;

    public void setCallback(Callback callback) {
        this.callback = callback;
    }

    private Callback callback;

    public LoginIMUtil(Activity activity) {
        this.activity = activity;
    }

    public void initIM() {
        InitBusiness.start(App.getContext());
        FriendshipEvent.getInstance().init();
        GroupEvent.getInstance().init();
        clearNotification();
    }

    public void onSuccess() {
        //初始化程序后台后消息推送
        PushUtil.getInstance();
        //初始化消息监听
        MessageEvent.getInstance();
        String deviceMan = Build.MANUFACTURER;
        //注册小米和华为推送
        if (deviceMan.equals("Xiaomi") && shouldMiInit()) {
            MiPushClient.registerPush(App.getContext(), "2882303761517480335", "5411748055335");
        } else if (deviceMan.equals("HUAWEI")) {
            com.huawei.android.pushagent.PushManager.requestToken(App.getContext());
        }
    }

    public void onError(int i, String s) {
        LogUtil.e("im登录错误: " + "code" + i + "--case:" + s);
    }

    /**
     * 判断小米推送是否已经初始化
     */
    private boolean shouldMiInit() {

        ActivityManager am = (ActivityManager) (activity.getSystemService(Context.ACTIVITY_SERVICE));
        List<ActivityManager.RunningAppProcessInfo> processInfos = am.getRunningAppProcesses();
        String mainProcessName = getPackageName();
        int myPid = android.os.Process.myPid();
        for (ActivityManager.RunningAppProcessInfo info : processInfos) {
            if (info.pid == myPid && mainProcessName.equals(info.processName)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 清楚所有通知栏通知
     */
    private void clearNotification() {
        NotificationManager notificationManager = (NotificationManager) activity
                .getSystemService(NOTIFICATION_SERVICE);
        notificationManager.cancelAll();
        MiPushClient.clearNotification(activity);
    }

    /**
     * 用户登录im
     * 这个方法写死了,有需要的话再修改
     */
    public void loginIM() {
        if (LoginInfo.getInstance(App.getContext()).isLogin()) {
            UserItem item = LoginInfo.getInstance(App.getContext()).getUser();
            String identifier = item.getU_id();
            String userSig = item.getImLoginSign();

            LoginBusiness.loginIm(identifier, userSig, new TIMCallBack() {
                @Override
                public void onError(int i, String s) {
                    LoginIMUtil.this.onError(i, s);
                }

                @Override
                public void onSuccess() {
                    LoginIMUtil.this.onSuccess();
                    if (callback != null) {
                        callback.next();
                    }
                }
            });
        }
    }

    public interface Callback {
        void next();
    }
}
